package com.szh.library.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.szh.common.enums.ResultCodeEnum;
import com.szh.common.result.Result;
import com.szh.library.user.service.UserService;
import com.szh.model.Book;
import com.szh.model.User;
import com.szh.vo.BookFindVo;
import com.szh.vo.UserInsertVo;
import com.szh.vo.UserModifyVo;
import com.szh.vo.UserSelectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: SongZiHao
 * @date: 2023/5/10
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping(value = "/library/user")
public class UserController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据id查询用户")
    @GetMapping(value = "/selectUserById/{id}")
    public Result<User> selectUserById(@ApiParam(name = "id", value = "用户id", required = true)
                                       @PathVariable("id") Long id) {
        User user = userService.selectUserById(id);
        return Result.build(user, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "查询所有用户")
    @GetMapping(value = "/selectAllUsers")
    public Result<List<User>> selectAllUsers() {
        List<User> users = userService.selectAllUsers();
        return Result.build(users, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "条件查询用户，带分页")
    @PostMapping(value = "/selectUsersByConditionPage/{page}/{limit}")
    public Result<Page<User>> selectUsersByConditionPage(@ApiParam(name = "page", value = "页码", required = true)
                                                         @PathVariable("page") Long page,
                                                         @ApiParam(name = "limit", value = "每页记录数", required = true)
                                                         @PathVariable("limit") Long limit,
                                                         @ApiParam(name = "userSelectVo", value = "查询条件vo类")
                                                         @RequestBody(required = false) UserSelectVo userSelectVo) {
        Page<User> pageModel = userService.selectUsersByConditionPage(page, limit, userSelectVo);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "新增用户")
    @PostMapping(value = "/insertUser")
    public Result insertUser(@ApiParam(name = "userInsertVo", value = "用户新增vo类", required = true)
                             @RequestBody UserInsertVo userInsertVo) {
        return userService.insertUser(userInsertVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id修改用户")
    @PutMapping(value = "/modifyUser")
    public Result modifyUser(@ApiParam(name = "userModifyVo", value = "用户修改vo类", required = true)
                             @RequestBody UserModifyVo userModifyVo) {
        return userService.modifyUser(userModifyVo) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping(value = "/removeUserById/{id}")
    public Result removeUserById(@ApiParam(name = "id", value = "待删用户id", required = true)
                                 @PathVariable("id") Long id) {
        return userService.removeUserById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "批量删除用户")
    @DeleteMapping(value = "/removeBatchByIds")
    public Result removeBatchByIds(@ApiParam(name = "userIds", value = "待删用户id集合", required = true)
                                   @RequestBody List<Long> userIds) {
        return userService.removeBatchByIds(userIds) ? Result.ok() : Result.fail();
    }

    //--------------------------- 以下接口只为回顾OpenFeign远程接口调用 ---------------------------

    @ApiOperation(value = "远程接口调用：根据id查询图书")
    @GetMapping(value = "/openFeign/book/findBookById/{id}")
    public Result<Book> openFeignFindBookById(@ApiParam(name = "id", value = "图书id", required = true)
                                              @PathVariable("id") Long id) {
        Book book = userService.openFeignFindBookById(id);
        return Result.build(book, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "远程接口调用：查询所有图书")
    @GetMapping(value = "/openFeign/book/findAllBooks")
    public Result<List<Book>> openFeignFindAllBooks() {
        List<Book> books = userService.openFeignFindAllBooks();
        return Result.build(books, ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "远程接口调用：条件查询图书，带分页")
    @PostMapping(value = "/openFeign/book/findBooksByConditionPage/{page}/{limit}")
    public Result<Page<Book>> openFeignFindBooksByConditionPage(@ApiParam(name = "page", value = "页码", required = true)
                                                                @PathVariable("page") Long page,
                                                                @ApiParam(name = "limit", value = "每页记录数", required = true)
                                                                @PathVariable("limit") Long limit,
                                                                @ApiParam(name = "bookFindVo", value = "查询条件vo类")
                                                                @RequestBody(required = false) BookFindVo bookFindVo) {
        Page<Book> pageModel = userService.openFeignFindBooksByConditionPage(page, limit, bookFindVo);
        return Result.ok(pageModel);
    }
}
